package cn.play.cardgame.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import java.util.HashMap;
import java.util.Map;

import cn.play.cardgame.R;

public class ImageUtil {
    private static ImageUtil mInstance;
    private static Map<Integer, Integer> PROGRESS_MAPPING;
    private static Map<Integer, Integer> COUNT_DOWN_MAPPING;
    private static Map<Integer, Integer> CONVERT_MAPPING;
    private static Map<Integer, Integer> LINE_COUNT_MAPPING;
    private static Map<Integer, Integer> BET_MAPPING;
    private static Map<Integer, Integer> INFO_MAPPING;
    private static Map<Integer, Integer> RATIO_MAPPING;

    public static ImageUtil getInstance() {
        if (mInstance == null)
            mInstance = new ImageUtil();
        return mInstance;
    }

    private ImageUtil() {

    }

    public static Map<Integer, Integer> getProgressMapping() {
        return PROGRESS_MAPPING;
    }

    public static Map<Integer, Integer> getCountDownMapping() {
        return COUNT_DOWN_MAPPING;
    }

    public static Map<Integer, Integer> getConvertMapping() {
        return CONVERT_MAPPING;
    }

    public static Map<Integer, Integer> getLineCountMapping() {
        return LINE_COUNT_MAPPING;
    }

    public static Map<Integer, Integer> getBetMapping() {
        return BET_MAPPING;
    }

    public static Map<Integer, Integer> getInfoMapping() {
        return INFO_MAPPING;
    }

    public static Map<Integer, Integer> getRatioMapping() {
        return RATIO_MAPPING;
    }

    static {
        PROGRESS_MAPPING = new HashMap<Integer, Integer>();
        PROGRESS_MAPPING.put(0, R.drawable.goldnum_0);
        PROGRESS_MAPPING.put(1, R.drawable.goldnum_1);
        PROGRESS_MAPPING.put(2, R.drawable.goldnum_2);
        PROGRESS_MAPPING.put(3, R.drawable.goldnum_3);
        PROGRESS_MAPPING.put(4, R.drawable.goldnum_4);
        PROGRESS_MAPPING.put(5, R.drawable.goldnum_5);
        PROGRESS_MAPPING.put(6, R.drawable.goldnum_6);
        PROGRESS_MAPPING.put(7, R.drawable.goldnum_7);
        PROGRESS_MAPPING.put(8, R.drawable.goldnum_8);
        PROGRESS_MAPPING.put(9, R.drawable.goldnum_9);

        RATIO_MAPPING = new HashMap<Integer, Integer>();
        RATIO_MAPPING.put(0, R.drawable.ratio_0);
        RATIO_MAPPING.put(1, R.drawable.ratio_1);
        RATIO_MAPPING.put(2, R.drawable.ratio_2);
        RATIO_MAPPING.put(3, R.drawable.ratio_3);
        RATIO_MAPPING.put(4, R.drawable.ratio_4);
        RATIO_MAPPING.put(5, R.drawable.ratio_5);
        RATIO_MAPPING.put(6, R.drawable.ratio_6);
        RATIO_MAPPING.put(7, R.drawable.ratio_7);
        RATIO_MAPPING.put(8, R.drawable.ratio_8);
        RATIO_MAPPING.put(9, R.drawable.ratio_9);
        COUNT_DOWN_MAPPING = new HashMap<Integer, Integer>();
        COUNT_DOWN_MAPPING.put(0, R.drawable.count_down0);
        COUNT_DOWN_MAPPING.put(1, R.drawable.count_down1);
        COUNT_DOWN_MAPPING.put(2, R.drawable.count_down2);
        COUNT_DOWN_MAPPING.put(3, R.drawable.count_down3);
        COUNT_DOWN_MAPPING.put(4, R.drawable.count_down4);
        COUNT_DOWN_MAPPING.put(5, R.drawable.count_down5);
        COUNT_DOWN_MAPPING.put(6, R.drawable.count_down6);
        COUNT_DOWN_MAPPING.put(7, R.drawable.count_down7);
        COUNT_DOWN_MAPPING.put(8, R.drawable.count_down8);
        COUNT_DOWN_MAPPING.put(9, R.drawable.count_down9);

        CONVERT_MAPPING = new HashMap<Integer, Integer>();
        CONVERT_MAPPING.put(0, R.drawable.red_xianhong0);
        CONVERT_MAPPING.put(1, R.drawable.red_xianhong1);
        CONVERT_MAPPING.put(2, R.drawable.red_xianhong2);
        CONVERT_MAPPING.put(3, R.drawable.red_xianhong3);
        CONVERT_MAPPING.put(4, R.drawable.red_xianhong4);
        CONVERT_MAPPING.put(5, R.drawable.red_xianhong5);
        CONVERT_MAPPING.put(6, R.drawable.red_xianhong6);
        CONVERT_MAPPING.put(7, R.drawable.red_xianhong7);
        CONVERT_MAPPING.put(8, R.drawable.red_xianhong8);
        CONVERT_MAPPING.put(9, R.drawable.red_xianhong9);

        LINE_COUNT_MAPPING = new HashMap<Integer, Integer>();
        LINE_COUNT_MAPPING.put(0, R.drawable.linenum0);
        LINE_COUNT_MAPPING.put(1, R.drawable.linenum1);
        LINE_COUNT_MAPPING.put(2, R.drawable.linenum2);
        LINE_COUNT_MAPPING.put(3, R.drawable.linenum3);
        LINE_COUNT_MAPPING.put(4, R.drawable.linenum4);
        LINE_COUNT_MAPPING.put(5, R.drawable.linenum5);
        LINE_COUNT_MAPPING.put(6, R.drawable.linenum6);
        LINE_COUNT_MAPPING.put(7, R.drawable.linenum7);
        LINE_COUNT_MAPPING.put(8, R.drawable.linenum8);
        LINE_COUNT_MAPPING.put(9, R.drawable.linenum9);

        BET_MAPPING = new HashMap<Integer, Integer>();
        BET_MAPPING.put(0, R.drawable.bet_num0);
        BET_MAPPING.put(1, R.drawable.bet_num1);
        BET_MAPPING.put(2, R.drawable.bet_num2);
        BET_MAPPING.put(3, R.drawable.bet_num3);
        BET_MAPPING.put(4, R.drawable.bet_num4);
        BET_MAPPING.put(5, R.drawable.bet_num5);
        BET_MAPPING.put(6, R.drawable.bet_num6);
        BET_MAPPING.put(7, R.drawable.bet_num7);
        BET_MAPPING.put(8, R.drawable.bet_num8);
        BET_MAPPING.put(9, R.drawable.bet_num9);

        INFO_MAPPING = new HashMap<Integer, Integer>();
        INFO_MAPPING.put(0, R.drawable.infopanel0);
        INFO_MAPPING.put(1, R.drawable.infopanel1);
        INFO_MAPPING.put(2, R.drawable.infopanel2);
        INFO_MAPPING.put(3, R.drawable.infopanel3);
        INFO_MAPPING.put(4, R.drawable.infopanel4);
        INFO_MAPPING.put(5, R.drawable.infopanel5);
        INFO_MAPPING.put(6, R.drawable.infopanel6);
        INFO_MAPPING.put(7, R.drawable.infopanel7);
        INFO_MAPPING.put(8, R.drawable.infopanel8);
        INFO_MAPPING.put(9, R.drawable.infopanel9);
        INFO_MAPPING.put(10, R.drawable.infopanel9);
    }

    public Bitmap mergeBitmapWithGap(Context context, int gap, int... imageIds) {
        Bitmap[] bitmaps = new Bitmap[imageIds.length];
        int width = 0;
        int maxHeight = 0;
        for (int i = 0; i < imageIds.length; i++) {
            bitmaps[i] = BitmapFactory.decodeResource(context.getResources(), imageIds[i]);
            width = width + bitmaps[i].getWidth() + gap;
            if (i == 0) {
                maxHeight = bitmaps[i].getHeight();
            } else {
                maxHeight = Math.max(maxHeight, bitmaps[i].getHeight());
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, maxHeight, bitmaps[0].getConfig());
        Canvas canvas = new Canvas(bitmap);
        int left = gap / 2;
        for (int i = 0; i < bitmaps.length; i++) {
            canvas.drawBitmap(bitmaps[i], left, 0, null);
            left += bitmaps[i].getWidth() + gap;
        }
        return bitmap;
    }

    public Bitmap mergeBitmap(Context context, int... imageIds) {
        Bitmap[] bitmaps = new Bitmap[imageIds.length];
        int width = 0;
        int maxHeight = 0;
        for (int i = 0; i < imageIds.length; i++) {
            bitmaps[i] = BitmapFactory.decodeResource(context.getResources(), imageIds[i]);
            width += bitmaps[i].getWidth();
            if (i == 0) {
                maxHeight = bitmaps[i].getHeight();
            } else {
                maxHeight = Math.max(maxHeight, bitmaps[i].getHeight());
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, maxHeight, bitmaps[0].getConfig());
        Canvas canvas = new Canvas(bitmap);
        int left = 0;
        for (int i = 0; i < bitmaps.length; i++) {
            if (i == 0) {
                canvas.drawBitmap(bitmaps[i], new Matrix(), null);
            } else {
                canvas.drawBitmap(bitmaps[i], left, 0, null);
            }
            left += bitmaps[i].getWidth();
        }
        return bitmap;
    }

    private int char2int(char c) {
        if (String.valueOf(c).equals("."))
            return 10;
        return Integer.parseInt(String.valueOf(c));
    }

    private Bitmap getMergeDrawable(Context context, Map<Integer, Integer> mapping, String str) {
        int len = str.length();
        char[] charArray = str.toCharArray();
        Bitmap bitmap = null;
        if (len == 1) {
            bitmap = mergeBitmap(context, mapping.get(char2int(charArray[0])));
        } else if (len == 2) {
            bitmap = mergeBitmap(context, mapping.get(char2int(charArray[0])), mapping.get(char2int(charArray[1])));
        } else if (len == 3) {
            bitmap = mergeBitmap(context, mapping.get(char2int(charArray[0])), mapping.get(char2int(charArray[1])),
                    mapping.get(char2int(charArray[2])));
        } else if (len == 4) {
            bitmap = mergeBitmap(context, mapping.get(char2int(charArray[0])), mapping.get(char2int(charArray[1])),
                    mapping.get(char2int(charArray[2])), mapping.get(char2int(charArray[3])));
        } else if (len == 5) {
            bitmap = mergeBitmap(context, mapping.get(char2int(charArray[0])), mapping.get(char2int(charArray[1])),
                    mapping.get(char2int(charArray[2])), mapping.get(char2int(charArray[3])),
                    mapping.get(char2int(charArray[4])));
        } else if (len == 6) {
            bitmap = mergeBitmap(context, mapping.get(char2int(charArray[0])), mapping.get(char2int(charArray[1])),
                    mapping.get(char2int(charArray[2])), mapping.get(char2int(charArray[3])),
                    mapping.get(char2int(charArray[4])), mapping.get(char2int(charArray[5])));
        } else if (len == 7) {
            bitmap = mergeBitmap(context, mapping.get(char2int(charArray[0])), mapping.get(char2int(charArray[1])),
                    mapping.get(char2int(charArray[2])), mapping.get(char2int(charArray[3])),
                    mapping.get(char2int(charArray[4])), mapping.get(char2int(charArray[5])),
                    mapping.get(char2int(charArray[6])));
        } else if (len == 8) {
            bitmap = mergeBitmap(context, mapping.get(char2int(charArray[0])), mapping.get(char2int(charArray[1])),
                    mapping.get(char2int(charArray[2])), mapping.get(char2int(charArray[3])),
                    mapping.get(char2int(charArray[4])), mapping.get(char2int(charArray[5])),
                    mapping.get(char2int(charArray[6])), mapping.get(char2int(charArray[7])));
        } else if (len == 9) {
            bitmap = mergeBitmap(context, mapping.get(char2int(charArray[0])), mapping.get(char2int(charArray[1])),
                    mapping.get(char2int(charArray[2])), mapping.get(char2int(charArray[3])),
                    mapping.get(char2int(charArray[4])), mapping.get(char2int(charArray[5])),
                    mapping.get(char2int(charArray[6])), mapping.get(char2int(charArray[7]))
                    , mapping.get(char2int(charArray[8])));
        }


        return bitmap;
    }

    private Bitmap getMergeDrawableWithGap(Context context, Map<Integer, Integer> mapping, String str, int gap) {
        int len = str.length();
        char[] charArray = str.toCharArray();
        Bitmap bitmap = null;
        if (len == 1) {
            bitmap = mergeBitmapWithGap(context, gap, mapping.get(char2int(charArray[0])));
        } else if (len == 2) {
            bitmap = mergeBitmapWithGap(context, gap, mapping.get(char2int(charArray[0])),
                    mapping.get(char2int(charArray[1])));
        } else if (len == 3) {
            bitmap = mergeBitmapWithGap(context, gap, mapping.get(char2int(charArray[0])),
                    mapping.get(char2int(charArray[1])), mapping.get(char2int(charArray[2])));
        } else if (len == 4) {
            bitmap = mergeBitmapWithGap(context, gap, mapping.get(char2int(charArray[0])),
                    mapping.get(char2int(charArray[1])), mapping.get(char2int(charArray[2])),
                    mapping.get(char2int(charArray[3])));
        } else if (len == 5) {
            bitmap = mergeBitmapWithGap(context, gap, mapping.get(char2int(charArray[0])),
                    mapping.get(char2int(charArray[1])), mapping.get(char2int(charArray[2])),
                    mapping.get(char2int(charArray[3])), mapping.get(char2int(charArray[4])));
        } else if (len == 6) {
            bitmap = mergeBitmapWithGap(context, gap, mapping.get(char2int(charArray[0])),
                    mapping.get(char2int(charArray[1])), mapping.get(char2int(charArray[2])),
                    mapping.get(char2int(charArray[3])), mapping.get(char2int(charArray[4])),
                    mapping.get(char2int(charArray[5])));
        } else if (len == 7) {
            bitmap = mergeBitmapWithGap(context, gap, mapping.get(char2int(charArray[0])),
                    mapping.get(char2int(charArray[1])), mapping.get(char2int(charArray[2])),
                    mapping.get(char2int(charArray[3])), mapping.get(char2int(charArray[4])),
                    mapping.get(char2int(charArray[5])), mapping.get(char2int(charArray[6])));
        }

        return bitmap;
    }

    public Drawable bitmapToDrawableWithGap(Context context, Map<Integer, Integer> mapping, String str, int gap) {
        Bitmap bitmap = getMergeDrawableWithGap(context, mapping, str, gap);
        if (bitmap != null)
            return new BitmapDrawable(context.getResources(), bitmap);
        return null;
    }

    public Drawable bitmapToDrawable(Context context, Map<Integer, Integer> mapping, String str) {
        Bitmap bitmap = getMergeDrawable(context, mapping, str);
        if (bitmap != null)
            return new BitmapDrawable(context.getResources(), bitmap);
        return null;
    }

    public Drawable bitmapToDrawable(Context context, Bitmap bitmap) {
        if (bitmap != null)
            return new BitmapDrawable(context.getResources(), bitmap);
        return null;
    }
}
